import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liuxlcommon/util/css_convert.dart';

class LStyle {
  String? css;
  double? radius;
  double? width;
  double? height;
  double? fontSize;
  int? maxLength;
  int? maxLines;
  bool? readOnly;
  FontWeight? fontWeight;
  String? hintText;
  TextAlign? textAlign;
  Decoration? decoration;
  Gradient? gradient;
  Gradient? textGradient;
  String? gradientCss;
  BoxBorder? border;
  BorderRadius? borderRadius;
  List<BoxShadow>? boxShadow;
  double? borderWidth;
  Color? borderColor;
  Rect? centerSlice;
  Color? color;
  bool? obscureText;
  Key? key;
  Color? hintColor;
  Color? backgroundColor;
  String? backgroundImage;
  Color? highlightColor;
  EdgeInsetsGeometry? padding;
  EdgeInsetsGeometry? margin;
  AlignmentGeometry? alignment;
  int? flex;
  bool showMaxLengthTxt;
  double? spaceBetween;
  MainAxisAlignment? mainAxisAlignment;
  CrossAxisAlignment? crossAxisAlignment;
  MainAxisSize? mainAxisSize;
  TextEditingController? editingController;
  GestureTapDownCallback? onTapDown;
  GestureTapCallback? onTap;
  Function(String text)? onChange;
  bool? isScroll;

  LStyle(
      {this.css,
      this.color,
      this.onTapDown,
      this.onChange,
      this.onTap,
      this.hintColor,
      this.isScroll,
      this.highlightColor,
      this.backgroundImage,
      this.padding,
      this.margin,
      this.border,
      this.key,
      this.editingController,
      this.borderRadius,
      this.centerSlice,
      this.boxShadow,
      this.alignment,
      this.flex,
      this.showMaxLengthTxt = false,
      this.gradient,
      this.gradientCss,
      this.borderWidth,
      this.maxLines,
      this.borderColor,
      this.maxLength,
      this.obscureText,
      this.fontWeight,
      this.hintText,
      this.textAlign,
      this.mainAxisSize,
      this.mainAxisAlignment,
      this.crossAxisAlignment,
      this.backgroundColor,
      this.radius,
      this.spaceBetween,
      this.decoration,
      this.width,
      this.readOnly,
      this.height,
      this.fontSize}) {
    cssConvert();
  }

  join(LStyle l) {}

  cssConvert() {
    if (css == null) return;
    List<String> lists = css!.split(';');
    for (var item in lists) {
      List<String> l = item.split(":");
      if (l.length < 2) continue;
      String key = l[0].trim();
      String value = l[1].trim();
      if (key == 'flex' && flex == null) {
        flex = int.parse(value);
      } else if (key == 'box-shadow' && boxShadow == null) {
        boxShadow = cssBoxShadow(value);
      } else if (key == 'font-weight' && fontWeight == null) {
        fontWeight = cssFontWeight(value);
      } else if (key == 'font-size' && fontSize == null) {
        fontSize = cssFontSize(value);
      } else if (key == 'width' && width == null) {
        width = cssPx(value);
      } else if (key == 'height' && height == null) {
        height = cssPx(value);
      } else if (key == 'color' && color == null) {
        color = cssColor(value);
      } else if (key == 'background-color' && backgroundColor == null) {
        backgroundColor = cssColor(value);
      } else if (key == 'background' && backgroundColor == null) {
        background(value);
      } else if (key == 'border' && border == null) {
        radius1(value);
      } else if (key == 'border-radius' && radius == null) {
        borderRadius1(value);
      } else if (key.startsWith('padding')) {
        padding ??= edgeInsets(key, value);
      } else if (key.startsWith('margin')) {
        margin ??= edgeInsets(key, value);
      }
    }
  }

  radius1(String value) {
    List<String> rs = value.split(" ");
    if (rs.length == 3) {
      border = Border.all(
          color: cssColor(rs[2]) ?? Colors.white, width: cssPx(rs[0]) ?? 0);
    }
  }

  borderRadius1(String value) {
    List<String> rs = value.split(" ");
    if (rs.length == 1) {
      radius = cssPx(value);
    } else if (rs.length == 4) {
      borderRadius = BorderRadius.only(
          topLeft: Radius.circular(cssPx(rs[0]) ?? 0),
          topRight: Radius.circular(cssPx(rs[1]) ?? 0),
          bottomLeft: Radius.circular(cssPx(rs[2]) ?? 0),
          bottomRight: Radius.circular(cssPx(rs[3]) ?? 0));
    }
  }

  background(String value) {
    if (value[0] == "#" || value.startsWith('rgba'))
      backgroundColor = cssColor(value);
    if (value.startsWith("linear-gradient"))
      gradient ??= convertCssGradient(value);
  }

  EdgeInsetsGeometry? edgeInsets(String key, String value) {
    List<String> parts = value.replaceAll('px', '').split(' ');
    if (parts.length == 0) return null;
    double top = 0;
    double right = 0;
    double bottom = 0;
    double left = 0;

    if (key.contains("top"))
      top = double.parse(parts[0]);
    else if (key.contains("right"))
      right = double.parse(parts[0]);
    else if (key.contains("bottom"))
      bottom = double.parse(parts[0]);
    else if (key.contains("left"))
      left = double.parse(parts[0]);
    else {
      top = double.parse(parts[0]);
      right = double.parse(parts.length > 1 ? parts[1] : parts[0]);
      bottom = double.parse(parts.length > 2 ? parts[2] : parts[0]);
      left = double.parse(parts.length > 3
          ? parts[3]
          : parts.length > 1
              ? parts[1]
              : parts[0]);
    }
    return EdgeInsets.fromLTRB(left.w, top.w, right.w, bottom.w);
  }
}
